import 'dart:async';
import 'dart:convert';

import 'package:client/common/event.dart';
import 'package:client/model/chat_msg_model.dart';
import 'package:client/model/chatroom_msg_model.dart';

class InfoRoute {
  static int respPing = 10;
  static int reqPong = 11;

  //请求
  //登录
  static int reqLogin = 100;
  //登出
  static int reqLogout = 500;
  //聊天室 获取在线
  static int reqChatroomMember = 102;
  //聊天室 发消息
  static int reqChatRoomSendInfo = 121;

  //私聊 发消息
  static int reqChatSendMsg = 122;

  //通知
  //登录
  static int respLogin = 200;
  //聊天室 在线列表
  static int respChatroomMember = 231;
  //聊天室 上线通知
  static int respChatroomOnline = 232;
  //聊天室 下线通知
  static int respChatroomOffline = 233;
  //聊天室 收到消息
  static int respChatRoomMsg = 234;

  //私聊 收到消息
  static int respChatMsg = 235;

  //路由
  static Map<int, Function> resp = {
    respPing: (String data) {
      bus.emit(EventBusSet.eventPing, "");
    },

    respLogin: (String data) {
      var payload = json.decode(data);
      bus.emit(EventBusSet.tcpLogin, payload);
    },

    //聊天室上线
    respChatroomOnline: (String data) {
      var payload = json.decode(data);
      bus.emit(EventBusSet.chatroomOnline, payload);
    },

    //聊天室下线
    respChatroomOffline: (String data) {
      var payload = json.decode(data);
      bus.emit(EventBusSet.chatroomOffline, payload);
    },

    //聊天室成员
    respChatroomMember: (String data) {
      var dataList = json.decode(data);
      bus.emit(EventBusSet.chatroomMember, dataList);
    },

    //聊天室消息
    respChatRoomMsg: (String data) {
      var dataMap = json.decode(data);

      var msgModel = ChatroomMsgModel();
      msgModel.name = dataMap["name"];
      msgModel.uuid = dataMap["uuid"];
      msgModel.msg = dataMap["msg"];
      bus.emit(EventBusSet.chatroomMsg, msgModel);
    },

    //私聊消息
    respChatMsg: (String data) {
      var dataMap = json.decode(data);
      print("chat msg 111");
      print(dataMap);

      var msgModel = ChatMsgModel();
      msgModel.talkId = dataMap["talk_id"];
      msgModel.name = dataMap["name"];
      msgModel.uuid = dataMap["uuid"];
      msgModel.msg = dataMap["msg"];

      bus.emit(EventBusSet.chatMsg, msgModel);
    },
  };

  static action(int cmd, String data) {
    print("cmd-->");
    print(cmd);
    print(data);

    var fn = resp[cmd];
    if (fn != null) {
      fn(data);
    }
  }
}
